#!/usr/bin/env python
#coding=utf-8

from BeautifulSoup import BeautifulSoup 
import urllib,time, datetime,socket
socket.setdefaulttimeout(5)

from pysqlite2 import dbapi2 as sqlite

import utils,settings, db
from ex import NoDataToday 

#url = "http://biz.finance.sina.com.cn/company/history.php?symbol=sh%s"

def get_date_sohustr(y,m,d):
    isostr = utils.get_date_isostr(y,m,d)
    return ''.join(isostr.split('-'))
def get_today_shoustr():
    isostr = utils.get_today_isostr()
    return ''.join(isostr.split('-'))

class Log4today:
    def __init__(self, fn):
        self.d = {}
        self.fn = fn
        
    def add(self, date, code):
        if not self.d.has_key(date): 
            self.d[date] = []
        self.d[date].append(code)
        
    def close(self):
        f = open(self.fn , 'a')
        for k in self.d.keys():
            f.write(k)
            f.write('\n')
            for c in self.d[k]:
                f.write(' %s\n'%c)
            f.write('\n')
        f.close()
        
def get_1day_data(date, code):
    items = ['open', 'high','low','close', 'volume']
    data = []
    result = {}
    
    u = url%code
    s = utils.get_soup(u)
    
    t = s.find(text=date)
    
    if not t:
        log.add(date, code) 
        raise NoDataToday(date, code)
    
    row = t.parent.parent
    for td in row.contents[2:7]:
        data.append(td.string)
        
    for k,v in zip(items, data):
        result[k] = v
        
    close = row.contents[5].contents[0].string
    result['close'] = close
    
    #164,983 -> 16498300
    v = result['volume']
    result['volume']= int(''.join(v.split(','))) * 100
    #
    result['date'] = date
    result['code'] = code
     
    return result

logdir = settings.root + settings.log + '/today.log'
log = Log4today(logdir)

url= "http://stock.business.sohu.com/q/hp.php?code=%s"

scoder = utils.get_scoder()
con = utils.get_db_connector()

cursor = con.cursor()

#sohudate = get_date_sohustr(2007, 7, 2)
#isodate = datetime.date(2007, 7, 2).isoformat()
isodate = utils.get_today_isostr()
sohudate = get_today_shoustr()

i = 0
while True:
    try:
        time.sleep(0.1)
        code = unicode(scoder.next()[0])
        print code
        #if code == '600003':break
    except KeyError:
        break
    
    try:
        data = get_1day_data(sohudate, code)
    except NoDataToday, e:
        continue
        print e
    except (socket.timeout,IOError), e:
        try:
            c.previous()
            # 如果 code 正好是第一个 :-( ...    
            except bsddb._bsddb.DBNotFoundError:
                c.close()
                c = utils.get_scoder()
            print '\n', code, ':', e, '\n'
            continue        
    else:
        data['date'] = isodate
        #print data
        db.insert_price_one(cursor, data)
        i = i + 1
        if i%10 == 0:
            con.commit() 
            print i, 'commited' 
                  
con.commit()
cursor.close()
con.close()
log.close()